package com.qida.clm.core.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.wifi.WifiInfo;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;

import java.util.UUID;

/**
 * Created by xiexiang on 2015/8/28.
 */
public final class DeviceUtils {
    private static final String TAG = DeviceUtils.class.getSimpleName();
    private static final String VIRTUAL_DEVICE_ID = "00000000000000";
    private static String sDeviceId;
    private static SharedPreferences mDevicePreferences;
    private static final String PREF_DEVICE_ID = "base_device_info";
    private static final String DEVICE_ID = "deviceId";

    private DeviceUtils(){}

    public static String getDeviceId(Context context){
        synchronized (DeviceUtils.class){
        	//先从内存中读取,不为空返回
            if(!isEmpty(sDeviceId))return sDeviceId;
            //为空,尝试从SharedPreferences中读取.
            sDeviceId = readDeviceIdForPref(context);
            //如果SharedPreferences读取为空,再去生成
            if(TextUtils.isEmpty(sDeviceId)){
                TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                sDeviceId = telephonyManager.getDeviceId();
                String androidId = Settings.Secure.getString(context.getContentResolver(),Settings.Secure.ANDROID_ID);
                if(VIRTUAL_DEVICE_ID.equals(sDeviceId)){
                    //模拟器设备
                    sDeviceId = UUID.randomUUID().toString();
                } else if(isEmpty(sDeviceId)){
                    sDeviceId = androidId;
                    if(isEmpty(sDeviceId)){
                        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1){
                            sDeviceId = Build.SERIAL;
                        } else {
                            sDeviceId = getMacAddress(context);
                        }
                    }
                }
                saveDeviceIdToPreference(context);
            }
            return sDeviceId;
        }
    }

    private static void ensurePreference(Context context){
    	if(mDevicePreferences == null){
            mDevicePreferences = 
            		SharePreferencesUtils.getSharedPreferences(context, PREF_DEVICE_ID);    		
    	}
    }

    private static String readDeviceIdForPref(Context context){
    	ensurePreference(context);
        return mDevicePreferences.getString(DEVICE_ID, "");
    }

    public static void saveDeviceIdToPreference(Context context){
    	ensurePreference(context);
    	Editor editor = mDevicePreferences.edit();
    	editor.putString(DEVICE_ID, sDeviceId);
    	editor.apply();
    }

    private static String getMacAddress(Context context){
        WifiInfo wifiInfo = SystemServiceUtils.getWifiManager(context).getConnectionInfo();
        if(wifiInfo != null){
            return wifiInfo.getMacAddress();
        }
        return "";
    }

    public static boolean isEmpty(String str){
        return TextUtils.isEmpty(str);
    }

	/** 获取设备信息*/
	public static String getDevicesInfo(){
		return String.format("%s-%s-%s-%s",
                Build.MANUFACTURER,Build.MODEL,getVersionSdk(),getAndroidVersion());
	}

    public static String getAndroidVersion(){
        return String.format("Android-%s",getVersionRelease());
    }

    public static String getVersionSdk(){
        return Build.VERSION.SDK;
    }

    public static String getVersionRelease(){
        return Build.VERSION.RELEASE;
    }

    public static boolean isPad(Context context) {
        WindowManager wm = SystemServiceUtils.getWindowManager(context);
        Display display = wm.getDefaultDisplay();
        // 屏幕宽度
        float screenWidth = display.getWidth();
        // 屏幕高度
        float screenHeight = display.getHeight();
        DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);
        double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
        double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
        // 屏幕尺寸
        double screenInches = Math.sqrt(x + y);
        // 大于6尺寸则为Pad
        if (screenInches >= 6.0) {
            return true;
        }
        return false;
    }

    /**
     * 是否为横屏
     * @param context
     * @return
     */
    public static boolean isHorizontalScreen(Context context){
        DisplayMetrics displayMetrics = getDisplayMetrics(context);
        return displayMetrics.widthPixels > displayMetrics.heightPixels;
    }

    public static boolean isVerticalScreen(Context context){
        DisplayMetrics displayMetrics = getDisplayMetrics(context);
        return displayMetrics.heightPixels > displayMetrics.widthPixels;
    }

    public static DisplayMetrics getDisplayMetrics(Context context){
        WindowManager windowManager = SystemServiceUtils.getWindowManager(context);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics;
    }
}